package com.itheima.service;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.MemberService;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.ChooseDateVo;
import com.itheima.pojo.DateCompare;
import com.itheima.pojo.DateCompareUtils;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member queryMember(String telephone) {
        //查询此电话号码是否是会员的

        //如果不是，就将此电话号码注册为会员

        Member member = orderDao.queryMemberById(telephone);

        //3、如果不是会员，直接注册为会员再进行预约，如果是会员则直接预约
        if (member == null){
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            /*添加新会员,这里让会员id进行回显，后面要用到*/
            orderDao.addMember(member);
        }
        return member;
    }

    /*查询上一年每月月底统计的会员数*/
    @Override
    public List<String> queryMemberByMonths(List<String> months) {

        List<String> monthCount =  new ArrayList<>();
        for (String month : months) {
            String end = month + ".31";
            Integer count = memberDao.queryMemberCountByMonth(end);
            monthCount.add(count.toString());
        }
        return monthCount;
    }

    /*根据会员id查询会员的家庭联系人信息*/
    @Override
    public List<Member> queryMemberFamily(Integer id) {
        return memberDao.queryMemberFamily(id);
    }

    @Override
    public List<Integer> getmemberCountByMonths(List<String> list) {
        List<Integer> list1 = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            for (String date : list) {
                date = date + ".31";
                Integer count = memberDao.getmemberCountByMonths(date);
                list1.add(count);
            }
        }
        return list1;
    }

    @Override
    public Result getMemberReport1(ChooseDateVo chooseDateVo) {
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        String startDateStr = chooseDateVo.getStartDate();
        String endDateStr = chooseDateVo.getEndDate();
        DateCompare dateCompare = DateCompareUtils.DateCompare(startDateStr, endDateStr);
        List<Integer> memberCount = new ArrayList<>();
        int startMonth = dateCompare.getStartMonth();
        int startYear = dateCompare.getStartYear();
        String dateStartStr = startYear + "." + startMonth + ".31";
        String dateEndStr = dateCompare.getEndYear() + "." + dateCompare.getEndMonth() + "." + dateCompare.getEndDate();
        if (startYear == dateCompare.getEndYear()) {
            if (dateCompare.getdValueMonth() == 0) {
                //说明他们同年月
                list.add("" + startYear + dateCompare.getStartMonth());
                int count = memberDao.getmemberCountByMonths(dateEndStr);
                memberCount.add(count);
                map.put("months", list);
                map.put("memberCount", memberCount);
                return Result.success("", map);
            }
            for (int i = 0; i < dateCompare.getdValueMonth() + 1; i++) {
                list.add(startYear + "." + startMonth);
                dateStartStr = startYear + "." + startMonth + ".31";
                int count = memberDao.getmemberCountByMonths(dateStartStr);
                memberCount.add(count);
                startMonth++;
            }
            map.put("months", list);
            map.put("memberCount", memberCount);
            return Result.success("", map);
        }
//            不是同一年

        for (int i = 0; i < dateCompare.getdValueMonth() + 1; i++) {
            if (startMonth > 12) {
                startMonth = 1;
                startYear = startYear+1;
            }
            list.add(startYear + "." + startMonth);
            dateStartStr = startYear + "." + startMonth + ".31";
            if(i==dateCompare.getdValueMonth()){
                int count = memberDao.getmemberCountByMonths(dateEndStr);
            }else {
                int count = memberDao.getmemberCountByMonths(dateStartStr);
                memberCount.add(count);
            }
            startMonth++;
        }
        map.put("months", list);
        map.put("memberCount", memberCount);
        return Result.success("", map);
    }

    @Override
    public Result getMemberMFReport() {
        Map<String, Object> map1 = new HashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<String> list = memberDao.getSex();
        List<String> list1 = new ArrayList<>();
        for (String sex : list) {
            Map<String, Object> map = new HashMap<>();
            if(sex.equals("1")){
                int count = memberDao.getmemberCountBySex(sex);
                list1.add("男");
                map.put("name", "男");
                map.put("value", count);
                mapList.add(map);
            }else {
                int count = memberDao.getmemberCountBySex(sex);
                list1.add("女");
                map.put("name", "女");
                map.put("value", count);
                mapList.add(map);
            }
        }

        map1.put("memberName", list1);
        map1.put("memberMFCount", mapList);

        return Result.success("",map1);

    }

    @Override
    public Result getAgeReport() {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<String> memberAge = new ArrayList<>();
        //0-18、18-30、30-45、45以上
        memberAge.add("0-18");
        memberAge.add("19-30");
        memberAge.add("31-45");
        memberAge.add("45以上");
        Integer[] arr = {0, 18, 19, 30, 31, 45, 46};
        int j = 0;
        for (Integer i = 0; i < arr.length; i++) {

            Map map1 = new HashMap<>();
            if(i==arr.length-1){
                map1.put("name", memberAge.get(j++));
                map1.put("value", memberDao.fidnCountByMaxAge(arr[i]));
                mapList.add(map1);
            }else {
                map1.put("name", memberAge.get(j++));
                map1.put("value", memberDao.findCountByAge(arr[i], arr[i + 1]));
                mapList.add(map1);}
            i++;
        }
        map.put("memberAge", memberAge);
        map.put("memberAgeCount", mapList);
        return Result.success("", map);
    }

    /**
     * 会员信息分页查询
     * @param currentPage 当前页数
     * @param pageSize  每页数据条数
     * @param queryString 查询条件
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        Page page = PageHelper.startPage(currentPage, pageSize);
        List<Member> memberList = memberDao.queryMemberAll(queryString);
        return new PageResult(page.getTotal(),memberList);
    }
}
